package cn.wsn2018.tools.dao.impl;


import cn.wsn2018.tools.dao.ProtocolConfigDao;
import cn.wsn2018.tools.model.*;
import cn.wsn2018.tools.model.Config.ProtocolConfig;
import cn.wsn2018.tools.model.Exception;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by NZX on 2018/12/20.
 */
@Repository("protocolConfigDao")
public class ProtocolConfigDaoImpl implements ProtocolConfigDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveProtocolConfig(ProtocolConfig protocolConfig) {
        if(protocolConfig.getIntegrity()==null){
            protocolConfig.setIntegrity(new Integrity());
        }
        if(protocolConfig.getAuthType()==""){
            protocolConfig.setAuthType("");
        }
        if(protocolConfig.getEncrypt()==null){
            protocolConfig.setEncrypt(new Encrypt());
        }
        if(protocolConfig.getException()==null){
            protocolConfig.setException(new Exception());
        }
        if(protocolConfig.getVirus()==null){
            protocolConfig.setVirus(new Virus());
        }
        if(protocolConfig.getResend()==null){
            protocolConfig.setResend(new Resend());
        }

        JSONObject result = JSONObject.fromObject(mongoTemplate.insert(protocolConfig,"protocolConfig"));
        String id = result.optString("id");
        if(id!=null&&!"".equals(id)) {
            return id;
        }
        else {
            return null;
        }
    }

    @Override
    public ProtocolConfig getProtocolConfig(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        ProtocolConfig protocolConfig = mongoTemplate.findOne(query,ProtocolConfig.class,"protocolConfig");
        return protocolConfig;
    }

    @Override
    public ProtocolConfig getProtocolConfigByDesignId(String designId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("designId").is(designId));
        ProtocolConfig protocolConfig = mongoTemplate.findOne(query,ProtocolConfig.class,"protocolConfig");
        return protocolConfig;
    }

    @Override
    public List<ProtocolConfig> getProtocolConfigs(String username, String pageNum, String pageSize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("username").is(username));
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        int num = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        query.skip((num-1)*size).limit(size);
        List<ProtocolConfig> protocolConfigs = mongoTemplate.find(query,ProtocolConfig.class,"protocolConfig");
        return protocolConfigs;
    }

    @Override
    public boolean updateProtocolConfig(ProtocolConfig config) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(config.getId()));
        Update update = new Update();
        update.set("createTime",config.getCreateTime());

        if(config.getAuthType()!=null){
            update.set("authType",config.getAuthType());
        }

        if(config.getEncrypt()!=null){
            update.set("encrypt",config.getEncrypt());
        }
        if(config.getException()!=null){
            update.set("exception",config.getException());
        }
        if(config.getVirus()!=null){
            update.set("virus",config.getVirus());
        }
        if(config.getResend()!=null){
            update.set("resend",config.getResend());
        }
        if(config.getIntegrity()!=null){
            update.set("integrity",config.getIntegrity());
        }
        JSONObject result = JSONObject.fromObject(mongoTemplate.updateFirst(query,update,"protocolConfig"));

        String count = result.optString("modifiedCount");
        if(count!=null&&"1".equals(count)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteProtocolConfig(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        JSONObject result = JSONObject.fromObject(mongoTemplate.remove(query,"protocolConfig"));
        String count = result.optString("deletedCount");
        if(count!=null&&"1".equals(count)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int deleteProtocolConfigs(List<String> idList) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").in(idList));
        JSONObject result = JSONObject.fromObject(mongoTemplate.remove(query,"protocolConfig"));
        String count = result.optString("deletedCount");
        return Integer.parseInt(count);
    }
}
